//
//  TextFieldAndTableCellViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/6/3.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "TextFieldAndTableCellViewController.h"
#import "TextFieldAndTableViewCell.h"

#pragma mark - ====================VM层====================
@interface TextFieldAndTableCellViewModel : NSObject
@property (copy, nonatomic) NSString *leftString;
@property (copy, nonatomic) NSString *rightString;
@property (strong, nonatomic) NSArray<TextFieldAndTableViewCellViewModel*> *dataArray;
@end
@implementation TextFieldAndTableCellViewModel
-(NSArray *)dataArray{
    if (!_dataArray) {
        NSMutableArray *tempt = [NSMutableArray array];
        for (NSInteger i = 0; i <= 10; i++) {
            TextFieldAndTableViewCellViewModel *data = [[TextFieldAndTableViewCellViewModel alloc] init];
            data.tag = i;
            [tempt addObject:data];
        }
        _dataArray = [NSArray arrayWithArray:tempt];
    }
    return _dataArray;
}
@end
#pragma mark - ====================V层====================

@interface TextFieldAndTableCellViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *leftTextField;
@property (weak, nonatomic) IBOutlet UITextField *rightTextField;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UIButton *clickButton;

@property(nonatomic, strong) TextFieldAndTableCellViewModel *viewModel;
@end

@implementation TextFieldAndTableCellViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self segInitViews];
    [self bindViewModel];
    [self.tableView reloadData];
}
#pragma mark - init Views
-(void)segInitViews{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TextFieldAndTableViewCell class] forCellReuseIdentifier:[TextFieldAndTableViewCell reuseIdentifier]];
}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    // 双向绑定
    RACChannelTo(self.viewModel,leftString) = self.leftTextField.rac_newTextChannel;
    [RACObserve(self.viewModel,leftString) subscribeNext:^(id  _Nullable x) {
        NSLog(@"leftString %@",x);
    }];
    
    [[self.clickButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.tableView endEditing:YES];
        for (TextFieldAndTableViewCellViewModel *object in self.viewModel.dataArray) {
            NSLog(@"inputValue %@",object.inputValue);
        }
    }];
}

#pragma mark - getter
-(TextFieldAndTableCellViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[TextFieldAndTableCellViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark - ====================delegate====================

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TextFieldAndTableViewCell *cell = [TextFieldAndTableViewCell cellWithTableView:tableView indexPath:indexPath];
    cell.viewModel = self.viewModel.dataArray[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
@end
